package main

import (
	"flag"
	"gark-elastic-job/core/impl"
	"github.com/olivere/elastic/v7"
	"time"

	"gark-elastic-job/core"
	"gark-elastic-job/etcd"
	"gark-elastic-job/util"

	"github.com/sirupsen/logrus"
)

const (
	DefaultEtcdEndpoints = "127.0.0.1:2379"
	DefaultTimeout       = 5000
)

var brokerId = flag.String("id", "", "the unique id of job node")

func main() {

	flag.Parse()
	if *brokerId == "" {
		logrus.Fatal("must specify a brokerId")
	}
	// 设置log日志的格式和其他配置
	//logrus.SetFormatter(&logrus.JSONFormatter{})
	logrus.SetLevel(logrus.DebugLevel)

	ip, err := util.GetLocalIPAddress()
	if err != nil {
		logrus.Fatal(err)
	}

	newEtcd, err := etcd.NewEtcd([]string{DefaultEtcdEndpoints}, DefaultTimeout*time.Millisecond)

	if err != nil {
		logrus.Fatalf("can't connect to newEtcd[%s] %v", DefaultEtcdEndpoints, err)
	}

	esClient, err := elastic.NewClient(elastic.SetSniff(false))
	if err != nil {
		logrus.Fatalf("failed to connect to es, err: %v", err)
	}

	// 启动节点, 在etcd上注册
	jobNode := core.NewJobNode(newEtcd, ip, *brokerId, impl.NewEsSaver(esClient, "gark-jobs"))
	jobNode.Bootstrap()

}
